﻿
$(document).ready(function () {
    GetCustomerDetail();
    ListLOV("Contact", "Address", "LoadAll");
    SetClick();
});

function SetClick() {
    $("#editCustomer").click(function () {
        GetCustomerDetail();
        ShowPopup("customerEditPopup");
    });
    $("#saveCustomer").click(function () {
        UpdateCustomer();
        $(".pop").fadeOut();
    });
    $("#additionalInfoAdd").click(function () {
        ShowPopup("additionalInfoPopup");
        ListRecordType();
        ListRecordName();
    });
    $("#Span_SaveAdditionalInfo").click(function () {
        AddLOV();
        $(".pop").fadeOut();
    });
    
//    $("#Select_RecordType").change(function () {
//        
//    });
}

function UpdateCustomer() {
    var CustomerId = getParameterByName("Id");
    var CustomerName = $("#Text_CustomerName").val();
    var list = [CustomerId, CustomerName];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "customerDetail.aspx/UpdateCustomer",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                GetCustomerDetail();            
                }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });

}

function GetCustomerDetail() {
    var CustomerId = getParameterByName("Id");
    var list = [CustomerId];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "customerDetail.aspx/GetCustomerDetail",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                SetCustomerDetail(result);
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}


function AddLOV() {
    var CustomerId = getParameterByName("Id");
    var RecordType = $("#Select_RecordType").val();
    var RecordName = $("#Select_RecordName").val();
    var RecordDescription = $("#Text_RecordDescription").val();
    var RecordValue = $("#Text_RecordValue").val();
    var list = [CustomerId, RecordType, RecordName, RecordDescription, RecordValue];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "customerDetail.aspx/AddLOV",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                ListLOV("Contact", "Address", "LoadAll");
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });

}

function ListLOV(RecordType,RecordName,LoadOption) {
    var CustomerId = getParameterByName("Id");
    var list = [CustomerId,RecordType,RecordName,LoadOption];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "customerDetail.aspx/ListLOV",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                ListAll(result);
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function ListAll(result) {
    var obj = jQuery.parseJSON(result);
    var customerInfo = $("#customerInfo");
    $(".listItemM").remove();
    for (var i = 0; i < obj.length; i++) {
        Title = obj[i].Description;
        Value = obj[i].RecordValue;
        var div = document.createElement("div");
        var deleteDiv = document.createElement("div");
        var editDiv = document.createElement("div");
        $(div).append("<b>"+Title+"</b>: " + Value);
        $(div).addClass("listItemM");
        $(deleteDiv).addClass("listItemDelete");
        $(editDiv).addClass("listItemEdit");
        $(div).append(editDiv);
        $(div).append(deleteDiv);        
        $(customerInfo).append(div);
    }
}

function ListRecordType() {    

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "customerDetail.aspx/ListRecordType",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                SetRecordtType(result);
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function ListRecordName() {
//    var SelectedValue = $('#Select_RecordType').val();
//    var list = [SelectedValue];
//    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
//        data: jsonText,
        url: "customerDetail.aspx/ListRecordName",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                SetRecordtName(result);
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function SetRecordtType(result) {
    var obj = jQuery.parseJSON(result);
    $('#Select_RecordType').empty();
    $('#Select_RecordType').append($('<option/>').attr("value", "0").text("Select"));
    $.each(obj, function (i, option) {
        $('#Select_RecordType').append($('<option/>').attr("value", option.KF).text(option.VF));
    });
}

function SetRecordtName(result) {
    var obj = jQuery.parseJSON(result);
    $('#Select_RecordName').empty();
    $('#Select_RecordName').append($('<option/>').attr("value", "0").text("Select"));
    $.each(obj, function (i, option) {
        $('#Select_RecordName').append($('<option/>').attr("value", option.KF).text(option.VF));
    });
}

function SetCustomerDetail(result) { 
    var obj = jQuery.parseJSON(result);
    $("#Text_CustomerName").val(obj.CustomerName);
    $("#Span_CustomerName").empty();
    $("#Span_CustomerName").append(obj.CustomerCode + "-" + obj.CustomerName);
}

function ShowPopup(elem) {
    $("#popupBg").fadeIn();
    $("#" + elem).fadeIn();   
}